package com.wjw.controller;

import com.wjw.entity.Collect;
import com.wjw.entity.Product;
import com.wjw.service.CollectService;
import com.wjw.service.ProductService;
import com.wjw.utils.CookieUtil;
import com.wjw.vo.RespBeanEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (`collect`)表控制层
 *
 * @author xxxxx
 */
@RestController
@RequestMapping("/collect")
public class CollectController {
    @Autowired
    private CollectService collectService;
    @Autowired
    private ProductService productService;

    /**
     *
     * @param paramMap
     * @param req
     * @return
     */
    @RequestMapping(value = "/getCollect")
    public Map getCollect(@RequestBody Map<String, Integer> paramMap, HttpServletRequest req) {
//        String ticket = CookieUtil.getCookieValue(req, "userTicket", false);
//        if (StringUtils.isEmpty(ticket) || req.getSession().getAttribute(ticket) == null) {
//            return RespBeanEnum.WITHOUT_LOGIN.getMap();
//        }
        Integer user_id = paramMap.get("user_id");
        HashMap<String, Object> map = new HashMap<>();
        if (user_id == null) {
            map.put("code", "004");
            map.put("msg", "用户未登录");
        } else {
            List<Collect> collectList = collectService.getCollect(user_id);
            if (collectList == null) {
                map.put("code", "002");
                map.put("msg", "该用户没有收藏的商品");
            } else {
                for (Collect collect : collectList) {
                    Product product = productService.getProductByID(collect.getProductId());
                    collect.setProductName(product.getProductName());
                    collect.setProductPicture(product.getProductPicture());
                    collect.setProductPrice(product.getProductPrice());
                    collect.setSellingPrice(product.getProductSellingPrice());
                    collect.setTitle(product.getProductTitle());
                }
                map.put("collectList", collectList);
                map.put("code", "001");
            }
        }
        return map;
    }

    /**
     *
     * @param paramMap
     * @param req
     * @return
     */
    @RequestMapping(value = "/addCollect")
    public Map addCollect(@RequestBody Map<String,Integer> paramMap, HttpServletRequest req){
//        String ticket = CookieUtil.getCookieValue(req, "userTicket", false);
//        if(StringUtils.isEmpty(ticket)||req.getSession().getAttribute(ticket)==null){
//            return RespBeanEnum.WITHOUT_LOGIN.getMap();
//        }
        Integer user_id = paramMap.get("user_id");
        Integer product_id = paramMap.get("product_id");
        HashMap<String, Object> map = new HashMap<>();
        if(collectService.findCollect(user_id,product_id)<1){   //未收藏
            collectService.addCollect(user_id,product_id);
            map.put("code","001");
            map.put("msg","收藏成功");
        }else{
            map.put("code","003");
            map.put("msg","该商品已经添加收藏，请到我的收藏查看");
        }
        return map;
    }

    /**
     *
     * @param paramMap
     * @param req
     * @return
     */
    @RequestMapping(value = "/deleteCollect")
    public Map deleteCollect(@RequestBody Map<String,Integer> paramMap, HttpServletRequest req){
        String ticket = CookieUtil.getCookieValue(req, "userTicket", false);
        if(StringUtils.isEmpty(ticket)||req.getSession().getAttribute(ticket)==null){
            return RespBeanEnum.WITHOUT_LOGIN.getMap();
        }
        Integer user_id = paramMap.get("user_id");
        Integer product_id = paramMap.get("productId");
        HashMap<String, String> map = new HashMap<>();
        if(collectService.findCollect(user_id,product_id)>0){   //收藏里存在
            collectService.deleteCollect(user_id,product_id);
            map.put("code","001");
            map.put("msg","取消收藏成功");
        }
        return map;
    }

}
